<template>
  <div class="mod-config">
    <el-form :inline="true" :model="queryParams" @keyup.enter.native="getDataList()" @submit.native.prevent
             size="small"
    >
      <el-form-item>
        <el-input v-model="queryParams.key" placeholder="参数名" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle(0)">新增
        </el-button>
        <el-button type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0"
        >批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        header-align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        label="折扣细则ID"
      >
      </el-table-column>
      <el-table-column
        prop="classificationName"
        header-align="center"
        label="菜品目录"
      >
      </el-table-column>
      <el-table-column
        prop="foodName"
        header-align="center"
        label="菜品名称"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        label="状态"
      >
        <template v-slot="{ row }">
          <el-tag type="success" v-show="row.status === 1">启用</el-tag>
          <el-tag type="danger" v-show="row.status === 2">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="discount"
        header-align="center"
        align="center"
        label="折扣"
      >
        <template v-slot="{ row }">
          <el-tag type="success">{{ row.discount }}折</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template v-slot="{ row }">
          <el-button type="text" size="small" @click="addOrUpdateHandle(row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="totalPage > 0"
      :total="totalPage"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList()"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './activityDiscountDetailAddOrUpdate.vue'
import { pageFoodDetail } from '@/api/marketing/activity/discount'

export default {
  data() {
    return {
      dataList: [],
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        discountId: undefined
      },
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList(id) {
      this.queryParams.discountId = id || undefined
      this.dataListLoading = true
      pageFoodDetail(this.queryParams).then(response => {
        this.dataList = response.rows
        this.totalPage = response.total
        this.dataListLoading = false
      })
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/store/tstorediscountdetail/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
